Package org.python.pydev.dltk.console.codegen

Source Code of org.python.pydev.dltk.console.codegen.StructuredSelectionGeneratorTestWorkbench$TestExceptionObject

package org.python.pydev.dltk.console.codegen;

import org.eclipse.jface.viewers.StructuredSelection;

import com.aptana.interactive_console.console.codegen.IScriptConsoleCodeGenerator;
import com.aptana.interactive_console.console.codegen.PythonSnippetUtils;
import com.aptana.interactive_console.console.codegen.SafeScriptConsoleCodeGenerator;
import com.aptana.interactive_console.console.codegen.StructuredSelectionScriptConsoleCodeGenerator;

import junit.framework.TestCase;

public class StructuredSelectionGeneratorTestWorkbench extends TestCase {

    /** An object that can be put into a Selection within this test */
    private static class TestSelectableObject implements IScriptConsoleCodeGenerator {
        private final String pyCode;
        private final boolean hasPyCode;

        /**
         * A version that has (if hasPyCode == true) a late realisation that it
         * can't generate the PyCode
         */
        public TestSelectableObject(boolean hasPyCode) {
            this.pyCode = null;
            this.hasPyCode = hasPyCode;
        }

        public TestSelectableObject(String pyCode) {
            this.pyCode = pyCode;
            this.hasPyCode = pyCode != null && pyCode.length() > 0;
        }

        public TestSelectableObject() {
            this.pyCode = null;
            this.hasPyCode = false;
        }

        public String getPyCode() {
            return pyCode;
        }

        public boolean hasPyCode() {
            return hasPyCode;
        }

    }

    /**
     * An object that can be put into a Selection within this test that throws
     * an exception on any call to getPyCode/hasPyCode
     */
    private static class TestExceptionObject implements IScriptConsoleCodeGenerator {
        public String getPyCode() {
            throw new RuntimeException("getPyCode Forced Failure");
        }

        public boolean hasPyCode() {
            throw new RuntimeException("hasPyCode Forced Failure");
        }

    }

    public void testGetAdapterForStructuredSelection() {
        IScriptConsoleCodeGenerator generator = PythonSnippetUtils
                .getScriptConsoleCodeGeneratorAdapter(StructuredSelection.EMPTY);
        assertEquals(StructuredSelectionScriptConsoleCodeGenerator.class, generator.getClass());
    }

    /**
     * Just to make our test cases smaller, extract the long method name as a
     * shorter one. Don't use the {@link SafeScriptConsoleCodeGenerator} wrapper
     * in here, we very much want to see the exceptions in the JUnit output
     */
    public IScriptConsoleCodeGenerator getGen(StructuredSelection selection) {
        return PythonSnippetUtils.getScriptConsoleCodeGeneratorAdapter(selection);
    }

    public void testEmptySelection() {
        assertEquals(false, getGen(StructuredSelection.EMPTY).hasPyCode());
        assertEquals(null, getGen(StructuredSelection.EMPTY).getPyCode());
    }

    public void testSingleSelection() {
        StructuredSelection selection = new StructuredSelection(new Object());
        assertEquals(false, getGen(selection).hasPyCode());
        assertEquals(null, getGen(selection).getPyCode());

        selection = new StructuredSelection(new TestSelectableObject());
        assertEquals(false, getGen(selection).hasPyCode());
        assertEquals(null, getGen(selection).getPyCode());

        selection = new StructuredSelection(new TestSelectableObject("pycode"));
        assertEquals(true, getGen(selection).hasPyCode());
        assertEquals("pycode", getGen(selection).getPyCode());

        selection = new StructuredSelection(new TestSelectableObject(true));
        assertEquals(true, getGen(selection).hasPyCode());
        assertEquals(null, getGen(selection).getPyCode());
    }

    public void testMultiSelection_NoPyCodeAvailable() {
        StructuredSelection selection = new StructuredSelection(new Object[] { new Object(), new Object() });
        assertEquals(false, getGen(selection).hasPyCode());
        assertEquals(null, getGen(selection).getPyCode());

        selection = new StructuredSelection(new Object[] { new Object(), new TestSelectableObject("pycode") });
        assertEquals(false, getGen(selection).hasPyCode());
        assertEquals(null, getGen(selection).getPyCode());

        selection = new StructuredSelection(new Object[] { new TestSelectableObject("pycode"), new Object() });
        assertEquals(false, getGen(selection).hasPyCode());
        assertEquals(null, getGen(selection).getPyCode());

        selection = new StructuredSelection(new Object[] { new TestSelectableObject(),
                new TestSelectableObject("pycode") });
        assertEquals(false, getGen(selection).hasPyCode());
        assertEquals(null, getGen(selection).getPyCode());

        selection = new StructuredSelection(new Object[] { new TestSelectableObject(), new TestSelectableObject() });
        assertEquals(false, getGen(selection).hasPyCode());
        assertEquals(null, getGen(selection).getPyCode());
    }

    public void testMultiSelection_LateNoPyCodeAvailable() {
        StructuredSelection selection = new StructuredSelection(new Object[] { new TestSelectableObject(true),
                new TestSelectableObject("pycode") });
        assertEquals(true, getGen(selection).hasPyCode());
        assertEquals(null, getGen(selection).getPyCode());
    }

    public void testMultiSelection_PyCodeAvailable() {
        StructuredSelection selection = new StructuredSelection(new Object[] { new TestSelectableObject("pycode1"),
                new TestSelectableObject("pycode2") });
        assertEquals(true, getGen(selection).hasPyCode());
        assertEquals("(pycode1, pycode2)", getGen(selection).getPyCode());
    }

    public void testNestedSelection() {
        StructuredSelection selection_inner = new StructuredSelection(new Object[] {
                new TestSelectableObject("pycode_inner1"), new TestSelectableObject("pycode_inner2") });
        StructuredSelection selection = new StructuredSelection(new Object[] { new TestSelectableObject("pycode1"),
                new TestSelectableObject("pycode2"), selection_inner });
        assertEquals(true, getGen(selection).hasPyCode());
        assertEquals("(pycode1, pycode2, (pycode_inner1, pycode_inner2))", getGen(selection).getPyCode());
    }

    public void testSafeRunner() {
        IScriptConsoleCodeGenerator generator = new TestExceptionObject();
        try {
            generator.hasPyCode();
            fail();
        } catch (RuntimeException e) {
            assertEquals("hasPyCode Forced Failure", e.getMessage());
        }
        try {
            generator.getPyCode();
            fail();
        } catch (RuntimeException e) {
            assertEquals("getPyCode Forced Failure", e.getMessage());
        }

        IScriptConsoleCodeGenerator wrapped = new SafeScriptConsoleCodeGenerator(generator);
        assertEquals(false, wrapped.hasPyCode());
        assertEquals(null, wrapped.getPyCode());

        StructuredSelection selection = new StructuredSelection(generator);
        IScriptConsoleCodeGenerator wrappedSelection = new SafeScriptConsoleCodeGenerator(getGen(selection));
        assertEquals(false, wrappedSelection.hasPyCode());
        assertEquals(null, wrappedSelection.getPyCode());
    }

}
TOP

Related Classes of org.python.pydev.dltk.console.codegen.StructuredSelectionGeneratorTestWorkbench$TestExceptionObject

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.